# XHR里get?csrf_token=
# 1.找到未加密的参数
# 2.想办法把参数进行加密，必须参考网易云的逻辑，params =>encText,encSecKey => encSecKey
# 3.请求到网易，拿到评论信息
import requests
# pip install pycryptodome
from Crypto.Cipher import AES
from base64 import b64encode
import json
url = "https://music.163.com/weapi/comment/resource/comments/get?csrf_token="
data = {
    "csrf_token": "",
    "cursor": "-1",
    "offset": "0",
    "orderType": "1",
    "pageNo": "1",
    "pageSize": "20",
    "rid": "R_SO_4_2617012458",
    "threadId": "R_SO_4_2617012458"
}

# 加密函数d传入的参数
# d:data数据,
e = "010001",
f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7",
g = "0CoJUm6Qyw8W8jud"
i = "zrpAqU3JbSrTwG9G"


# i固定为zrpAqU3JbSrTwG9G时,固定得到encSecKey

# 转化成16的倍数，为下方的加密算法服务
def to_16(data):
    pad = 16 - len(data) % 16
    data += chr(pad) * pad
    return  data
def get_encSecKey():
    return "5e9a2a9fd9a1a0d3289260f3a3a57248e0b3179ee3a4b8ccad5b4d7c2e5ce520f5060bd419b78670ce5bec164a88a1c4df923f93707641525413f4145380bfa9e3ba8058ba3d6dc6d72a9a984b3d2e7fdacf2d55f872f468a10578d9224fc125236507bdcddae810d08aff0ec5bf5ddd7e1aa7050613a2ad8c701d3ac47b51e4"

def get_params(data):
    first = enc_params(data, g)
    second = enc_params(first, i)
    return second


# 加密过程
def enc_params(data, key):
    iv = "0102030405060708"
    data = to_16(data)
    # 创建加密器
    aes = AES.new(key=key.encode("utf-8"), IV=iv.encode("utf-8"), mode=AES.MODE_CBC)
    # 加密，加密的内容的长度必须是16的倍数
    bs = aes.encrypt(data.encode("utf-8"))
    # 转化成字符串加密返回
    return str(b64encode(bs), "utf-8")



# 处理加密过程
# window.asrsea=d
# bse3x(["流泪", "强"])等复制到console里，出现警告手动输入allow pasting并按Enter键
# var bVi2x = window.asrsea(JSON.stringify(i8a), bse3x(["流泪", "强"]), bse3x(Qu8m.md), bse3x(["爱心", "女孩", "惊恐", "大笑"])


"""
function a(a) {
        var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
        for (d = 0; a > d; d += 1)
            e = Math.random() * b.length,
            e = Math.floor(e),
            c += b.charAt(e);
        return c
    }
    function b(a, b) {
        var c = CryptoJS.enc.Utf8.parse(b)
          , d = CryptoJS.enc.Utf8.parse("0102030405060708")
          , e = CryptoJS.enc.Utf8.parse(a)
          , f = CryptoJS.AES.encrypt(e, c, {
            iv: d,
            mode: CryptoJS.mode.CBC
        });
        return f.toString()
    }
    function c(a, b, c) {
        var d, e;
        return setMaxDigits(131),
        d = new RSAKeyPair(b,"",c),
        e = encryptedString(d, a)
    }
    function d(d, e, f, g) {
        var h = {}
          , i = a(16);     //i是16位的随机数
        return h.encText = b(d, g),
        h.encText = b(h.encText, i),  //返回的是params
        h.encSecKey = c(i, e, f),     //得到encSecKey
        h
    }
    function e(a, b, d, e) {
        var f = {};
        return f.encText = c(a + e, b, d),
        f
    }
"""
resp = requests.post(url, data={
    "params": get_params(json.dumps(data)),
    "encSecKey": get_encSecKey()
})
print(resp.json())
